import { guarded, json } from "@/lib/api"; import { readAttachment, deleteAttachment, getAttachment } from "@/lib/storage"; export const GET = guarded<{ id: string }>(async (req, { params }) => { if (req.nextUrl.searchParams.get("meta") === "1") { const a = await getAttachment(params.id); return a ? json({ attachment: { ...a, extracted_text: a.extracted_text?.slice(0, 20_000) } }) : json({ error: "Introuvable" }, { status: 404 }); } const a = await readAttachment(params.id); if (!a) return json({ error: "Introuvable" }, { status: 404 }); const download = req.nextUrl.searchParams.get("download") === "1"; return new Response(new Uint8Array(a.data), { headers: { "content-type": a.mime, "content-length": String(a.data.length), "cache-control": "private, max-age=3600", "content-disposition": `${download ? "attachment" : "inline"}; filename*=UTF-8''${encodeURIComponent(a.name)}`, "x-content-type-options": "nosniff" } }); }); export const DELETE = guarded<{ id: string }>(async (_req, { params }) => { await deleteAttachment(params.id); return json({ ok: true }); });